package com.smart.dao;

import com.smart.domain.Recorder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@Repository

public class RecorderDao {

        private JdbcTemplate jdbcTemplate;

        @Autowired // 自动注入jdbcTemplate 的bean
        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }
        //插入
        public int  addRecorder(  Recorder recorder) {

            final  String sql = "insert into recorder( recorder_teacherName,recorder_courseName,recorder_computerRoom,recorder_lesson,recorder_classes,recorder_week,recorder_weekNumber,recorder_startTime,recorder_stopTime) VALUES(?,?,?,?,?,?,?,?,?)";
            Object[] params= new Object[]{recorder.getRecorder_teacherName(), recorder.getRecorder_courseName(), recorder.getRecorder_computerRoom(), recorder.getRecorder_lesson(),recorder.getRecorder_classes(),recorder.getRecorder_week(),recorder.getRecorder_weekNumber(),recorder.getRecorder_startTime(),recorder.getRecorder_stopTime()};
            return jdbcTemplate.update(sql,params);
        }
        /**
         * 修改，但我发现主码跟唯一值不能修改
         * @param
         * @param
         * @ return
         */
        public int updateRecorder( Recorder recorder){
            final String sqlt = "update recorder set recorder_week= ? where recorder_weekNumber= ?";
            return jdbcTemplate.update(sqlt,recorder.getRecorder_week(),recorder.getRecorder_weekNumber());

        }
        /**
         * 删除
         * @param
         * @param
         * @ return
         */
        public int deleteRecorder(Recorder recorder){
            final String sql ="delete from recorder where recorder_lesson =?";
            return jdbcTemplate.update(sql,recorder.getRecorder_lesson());
        }



        public void queryRecorder(Recorder recorderone)
        {
            String sql ="select * from recorder where recorder_computerRoom = ? ";
            Recorder com =jdbcTemplate.queryForObject(sql,new MyRowMapperone(),recorderone.getRecorder_computerRoom());
            System.out.println(com);
        }



        /**
         * 查询集合对象
         * @param
         * @param
         * @ return
         */
        public void querylistRecorder(Recorder recorder){
            String sql ="select * from recorder";
            List<Recorder> com =jdbcTemplate.query(sql,new MyRowMapperone());
            System.out.println(com);
        }
    }


    /**
     封装MyRowMaper类
     @param
     @param
     @ return */
    class MyRowMapperone implements RowMapper<Recorder> {
        @Override
        public Recorder mapRow(ResultSet rs, int i) throws SQLException {
            //1 从结果集里得到数据
            String teacherName =rs.getString("recorder_teacherName");
            String courseName=rs.getString("recorder_courseName");
            String computerRoom=rs.getString("recorder_computerRoom");
            String lesson =rs.getString("recorder_lesson");
            String classes =rs.getString("recorder_classes");
            int week=rs.getInt("recorder_week");
            int weekNumber=rs.getInt("recorder_weekNumber");
            Date startTime=rs.getDate("recorder_startTime");
           Date stopTime=rs.getDate("recorder_stopTime");

            //2 把得到数据封装到数据里面去
            Recorder com =new Recorder();
            com.setRecorder_teacherName(teacherName);
            com.setRecorder_courseName(courseName);
            com.setRecorder_computerRoom(computerRoom);
            com.setRecorder_lesson(lesson);
            com.setRecorder_classes(classes);
            com.setRecorder_week(week);
            com.setRecorder_weekNumber(weekNumber);
            com.setRecorder_startTime(startTime);
            com.setRecorder_stopTime(stopTime);
            return com;
        }
    }


